package gac.helloworld;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AlarmListActivity extends ListActivity {
	private static final int ACTIVITY_CREATE=0;
    private static final int ACTIVITY_EDIT=1;

    private static final int INSERT_ID = Menu.FIRST;
    private static final int DELETE_ID = Menu.FIRST + 1;

    private static final int MAX_NUM_ALARMS = 1000;
    
    private AlarmDbAdapter mDbHelper;
	private ArrayList<AlarmData> mAlarmData = null;
	
	// maps arraylist indices to database ids
	private int[] indexIdPairs;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        
        indexIdPairs = new int[MAX_NUM_ALARMS];
        
        // get stored alarm data
        mDbHelper = new AlarmDbAdapter(this);
        mDbHelper.open();
        //createTestData();
        fillAlarmData();
        
                
        ListView lv = getListView();
        lv.setItemsCanFocus(true);  // necessary for item selection to work
            
        // add the "create alarm" button
        LayoutInflater inflater = getLayoutInflater();
        ViewGroup alarm_bar = (ViewGroup) inflater.inflate(R.layout.alarm_bar, lv, false);
        
        lv.addFooterView(alarm_bar, null, false);     
        
        AlarmAdapter mAdapter = new AlarmAdapter(this, R.layout.alarm_row, mAlarmData);
        setListAdapter(mAdapter);                               
    }

    private void createTestData() {
    	mDbHelper.createAlarm("hi", 1, 2, 3, AlarmData.PM);
    }
    
    private class AlarmAdapter extends ArrayAdapter<AlarmData> {

    	private ArrayList<AlarmData> items;

    	public AlarmAdapter(Context context, int textViewResourceId, ArrayList<AlarmData> items) {
    		super(context, textViewResourceId, items);
    		this.items = items;
    	}

    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		final int position_final = position;
    		View v = convertView;
    		if (v == null) {
    			LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    			v = vi.inflate(R.layout.alarm_row, null);
    		}
    		AlarmData o = items.get(position);
    		if (o != null) {
    			TextView destTV = (TextView) v.findViewById(R.id.destination_text);
    			TextView arrivalTimeTV= (TextView) v.findViewById(R.id.arrival_time_text);
    			TextView alarmTimeTV= (TextView) v.findViewById(R.id.alarm_time_text);
    			
    			if(destTV != null){
    				destTV.setText(getString(R.string.destination) + ":\t" + o.getDestination());
    			}    			
    			if (arrivalTimeTV != null) {
    				arrivalTimeTV.setText(getString(R.string.arrival_time) + ":\t" + o.getArrivalTimeString());
    			}
    			if (alarmTimeTV != null) {
    				alarmTimeTV.setText(getString(R.string.alarm_time) +  ":\t" + o.getAlarmTimeString());
    			}   			
    		}
    		v.setClickable(true);
    		v.setFocusable(true);
    		v.setBackgroundResource(android.R.drawable.menuitem_background);
    		v.setOnClickListener(new OnClickListener() {

    			@Override
    			public void onClick(View v) {
    				editAlarm(items.get(position_final).getId());
    			}

    		});
    		return v;
    	}
    }    
    
    // make a database call to populate mAlarmData
    private void fillAlarmData() {      
    	mAlarmData = new ArrayList<AlarmData>();
        Cursor cur = mDbHelper.fetchAllAlarms();
        startManagingCursor(cur);               

        // read data out of the database
        if (cur.moveToFirst()) {
        	do {
        		// Get the field values
        		long id = cur.getLong(cur.getColumnIndex(AlarmDbAdapter.KEY_ROWID));        		        		
        		String dest = cur.getString(cur.getColumnIndex(AlarmDbAdapter.KEY_DESTINATION));
        		int hour = cur.getInt(cur.getColumnIndex(AlarmDbAdapter.KEY_HOUR));
        		int minute = cur.getInt(cur.getColumnIndex(AlarmDbAdapter.KEY_MINUTE));
        		int warntime = cur.getInt(cur.getColumnIndex(AlarmDbAdapter.KEY_WARNTIME));
        		int ampm = cur.getInt(cur.getColumnIndex(AlarmDbAdapter.KEY_AMPM));

        		mAlarmData.add(new AlarmData(id, dest, hour, minute, ampm, warntime));
        	} while (cur.moveToNext());
        }
    	
    	// create an adapter and set it to display
        AlarmAdapter mAdapter = new AlarmAdapter(this, R.layout.alarm_row, mAlarmData);
        setListAdapter(mAdapter);  
        
    }
    
    // launch the gps in response to a "show map" click
    	
    public void launchGPS(View v) {
    	
    }

    // create an alarm in response to a "create alarm" click
    public void editAlarm(Long id) {
		//Toast.makeText(this, "eessdfh: " + id, Toast.LENGTH_LONG).show();    	
    	Intent i = new Intent(getApplicationContext(), MainActivity.class);
    	i.putExtra(AlarmDbAdapter.KEY_ROWID, id);
    	startActivityForResult(i, ACTIVITY_EDIT);
    }
    
    public void createAlarm(View v)
    {
    	Intent i = new Intent(getApplicationContext(), MainActivity.class);
    	startActivityForResult(i, ACTIVITY_CREATE);
    }    
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        fillAlarmData();
    }    
}